package org.sanm.webSocket;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.sanm.utils.SpringUtil;
import org.springframework.web.socket.WebSocketSession;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Room {
    public static final Map<String, Room> roomMap = new ConcurrentHashMap<>();
    public static final Map<String, String> userToRoom = new ConcurrentHashMap<>();

    private String code;
    private List<Member> memberList = new ArrayList<>();

    public static Room createRoom(Message message) throws Exception {
        Room room;
        String targetId = message.getTargetId();
        synchronized (targetId) {
            String senderId = message.getSenderId();

            room = Room.get(targetId);
            if(room == null){
                room = new Room();
                room.setCode(targetId);
                roomMap.put(targetId, room);
                WebSocketHandler.sendMessage(senderId, new Message(MessageType.CREATE_ROOM, "服务器", "服务器", senderId, "创建房间成功"));
            } else {
                Message m = new Message(MessageType.CREATE_ROOM, "服务器", "服务器", senderId, "房间已存在");
                m.setCode(1);
                WebSocketHandler.sendMessage(senderId, m);
            }
        }


        return room;
    }

    public static Room get(String code){
        return roomMap.get(code);
    }

    public static void joinRoom(Message message) throws Exception{

        String senderId = message.getSenderId();

        String targetId = message.getTargetId();
        Room room = Room.get(targetId);
        if(room == null){
            Message m = new Message(MessageType.JOIN_ROOM, "服务器", "服务器", senderId, "加入的房间不存在");
            m.setCode(1);
            WebSocketHandler.sendMessage(senderId, m);
            return;
        }
        Member member = new Member(senderId, message.getSenderName());
        room.getMemberList().add(member);

        WebSocketHandler.sendMessage(senderId, new Message(MessageType.JOIN_ROOM, "服务器", "服务器", senderId, "加入房间成功"));
        for(Member me : room.getMemberList()){

            WebSocketHandler.sendMessage(me.getId(), new Message(MessageType.MEMBER_JOIN, "服务器", "服务器", me.getId(), member));

        }

        userToRoom.put(member.getId(), targetId);

    }



    public static Room exitRoom(String code, String userId) throws Exception {
        Room room = get(code);
        if(room == null){
            return room;
        }
        List<Member> memberList = room.getMemberList();
        for(int i = memberList.size() - 1; i >=0; i--){
            Member member = memberList.get(i);
            if(member.getId().equals(userId)){
                memberList.remove(i);
            } else {

                WebSocketHandler.sendMessage(member.getId(), new Message(MessageType.MEMBER_EXIT, "服务器", "服务器", member.getId(), userId));
            }
        }
        userToRoom.remove(userId);


        return room;
    }

    public static Room exitRoom(String userId) throws Exception {
        String code = userToRoom.get(userId);
        return exitRoom(code, userId);
    }

    public static void handleRoomMessage(Message message) throws Exception {
        String targetId = message.getTargetId();
        Room room = Room.get(targetId);
        List<Member> memberList = room.getMemberList();

        for(int i = memberList.size() - 1;  i >=0; i--){
            Member m = memberList.get(i);

            WebSocketHandler.sendMessage(m.getId(), message);

        }
    }
}
